import CryptoJS from 'crypto-js';
// @ts-ignore
import {JSEncrypt} from "encryptlong";
import {useCryptStore} from "../store/crypt";

const $crypt = useCryptStore();

export function encryptRsa(publicKey: string, data: string) {
    let encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    return encrypt.encryptLong(data)
}

export function decryptRsa(privateKey: string, data: string) {
    let decrypt = new JSEncrypt();
    decrypt.setPrivateKey(privateKey);
    return decrypt.decryptLong(data)
}

export function encryptAes(data: any) {
    if (typeof(data) === 'object') {
        data = JSON.stringify(data);
    }
    return CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse($crypt.aes), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.ZeroPadding,
    }).toString();
}

export function decryptAes(data: string) {
    let decrypt = CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse($crypt.aes), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.ZeroPadding,
    });
    return CryptoJS.enc.Utf8.stringify(decrypt);
}
